<?php

/*
* Update the contents of all pile divs.
*
* @header project_name Name of the project
* @header note_start_kids Number of notes in initial div
* @header one_point_main_kids Number of notes in first div
* @header two_points_main_kids Number of notes in second div
* @header three_points_main_kids Number of notes in third div
* @header five_points_main_kids Number of notes in fourth div
* @header eight_points_main_kids Number of notes in fifth div
* @header thirteen_points_main_kids Number of notes in last, sixth div
*
* @return mysql_error()
*          if there was an error updating the database
* @return "success"
*          upon success
*/

require_once("session.php");
require_once("connect_to_database.php");

$project = $_POST['project_name'];

$start = $_POST['note_start_kids'];
$start_list = implode(",", $start);

$one = $_POST['one_point_main_kids'];
$one_list = implode(",", $one);

$two = $_POST['two_points_main_kids'];
$two_list = implode(",", $two);

$three = $_POST['three_points_main_kids'];
$three_list = implode(",", $three);

$five = $_POST['five_points_main_kids'];
$five_list = implode(",", $five);

$eight = $_POST['eight_points_main_kids'];
$eight_list = implode(",", $eight);

$thirteen = $_POST['thirteen_points_main_kids'];
$thirteen_list = implode(",", $thirteen);


// Update initial div
$query0 = sprintf("UPDATE points SET divChildren='%s', divChildrenList='%s' WHERE projectName='%s' AND divID='note_start'",
                 count($start),$start_list,$project);
$result = mysql_query($query0);

if(!$result){
	error_log($query.mysql_error());
	die(mysql_error());
}
echo("success");

// Update first div
$query1 = sprintf("UPDATE points set divChildren='%s', divChildrenList='%s' WHERE projectName='%s' AND divID='one_point_main'",
                 count($one),$one_list,$project);
$result = mysql_query($query1);

if(!$result){
	error_log($query.mysql_error());
	die(mysql_error());
}
echo("success");

// Update second div
$query2 = sprintf("UPDATE points set divChildren='%s', divChildrenList='%s' WHERE projectName='%s' AND divID='two_points_main'",
                 count($two),$two_list,$project);
$result = mysql_query($query2);

if(!$result){
	error_log($query.mysql_error());
	die(mysql_error());
}
echo("success");

// Update third div
$query3 = sprintf("UPDATE points set divChildren='%s', divChildrenList='%s' WHERE projectName='%s' AND divID='three_points_main'",
                 count($three),$three_list,$project);
$result = mysql_query($query3);

if(!$result){
	error_log($query.mysql_error());
	die(mysql_error());
}
echo("success");

// Update fourth div
$query4 = sprintf("UPDATE points set divChildren='%s', divChildrenList='%s' WHERE projectName='%s' AND divID='five_points_main'",
                 count($five),$five_list,$project);
$result = mysql_query($query4);

if(!$result){
	error_log($query.mysql_error());
	die(mysql_error());
}
echo("success");

// Update fifth div
$query5 = sprintf("UPDATE points set divChildren='%s', divChildrenList='%s' WHERE projectName='%s' AND divID='eight_points_main'",
                 count($eight),$eight_list,$project);
$result = mysql_query($query5);

if(!$result){
	error_log($query.mysql_error());
	die(mysql_error());
}
echo("success");

// Update sixth div
$query6 = sprintf("UPDATE points set divChildren='%s', divChildrenList='%s' WHERE projectName='%s' AND divID='thirteen_points_main'",
                 count($thirteen),$thirteen_list,$project);
$result = mysql_query($query6);

if(!$result){
	error_log($query.mysql_error());
	die(mysql_error());
}
echo("success");
?>